﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaseData;
using BaseData.Exts;
using BaseLib.Helper;
namespace BaseUtility
{
    public class AccExts : Base
    {
        public AccExts()
        {
        }
        private Enums.AccType _cookieName = Enums.AccType.Unknow;
        // private static UserExt Instances { get; set; }
        public AccExts(Enums.AccType cookieName)
        {
            _cookieName = cookieName;
            // Instances = new UserExt(cookieName);
        }
        public AccClass Instances
        {
            get
            {
                switch (_cookieName)
                {
                    case Enums.AccType.Super:
                        return Get_AccByCookie(Enums.AccType.Super);
                    case Enums.AccType.Staff:
                        return Get_AccByCookie(Enums.AccType.Staff);
                    case Enums.AccType.Member:
                        return Get_AccByCookie(Enums.AccType.Member);
                    case Enums.AccType.Vip:
                        return Get_AccByCookie(Enums.AccType.Vip);
                    case Enums.AccType.Biz:
                        return Get_AccByCookie(Enums.AccType.Biz);
                    case Enums.AccType.User:
                    default:
                        return Get_AccByCookie(Enums.AccType.User);
                }
            }
        }
        private static AccClass Get_AccByCookie(Enums.AccType cookieName)
        {
            var accx = new AccClass();
            var accs = AccCookies.GetAccCookies(cookieName);
            using (var ctx = new ContextExt())
            {
                if (!accs.Any()) return accx;
                var acc = accs.FirstOrDefault();
                if (acc == null) return accx;
                IQueryable<AccClass> sql;
                switch (cookieName)
                {
                    case Enums.AccType.Staff:
                        sql = ctx.Web_Base_Account_Staff.Select(q => new AccClass
                        {
                            Id = q.PkId,
                            Name = q.Name,
                            Type = acc.Type,
                        });
                        break;
                    case Enums.AccType.Super:
                        sql = ctx.Web_Base_Account_Super.Select(q => new AccClass
                        {
                            Id = q.PkId,
                            Name = q.Name,
                            Type = acc.Type,
                        });
                        break;
                    case Enums.AccType.Member:
                        sql = ctx.Web_Base_Account_Member.Select(q => new AccClass
                        {
                            Id = q.PkId,
                            Name = q.Name,
                            Type = acc.Type,
                        });
                        break;
                    case Enums.AccType.Vip:
                        sql = ctx.Web_Base_Account_Vip.Select(q => new AccClass
                        {
                            Id = q.PkId,
                            Name = q.Name,
                            Type = acc.Type,
                        });
                        break;
                    case Enums.AccType.Biz:
                        sql = ctx.Web_Base_Account_Biz.Select(q => new AccClass
                        {
                            Id = q.PkId,
                            Name = q.Name,
                            Type = acc.Type,
                        });
                        break;
                    case Enums.AccType.User:
                    default:
                        sql = ctx.Web_Base_Account_User.Select(q => new AccClass
                        {
                            Id = q.PkId,
                            Name = q.Name,
                            Type = acc.Type,
                        });
                        break;
                }
                sql = sql.Where(w => w.Name == acc.Name);
                // Response.Write(sql);
                return sql.Any() ? sql.ToList().FirstOrDefault() : new AccClass();
                /*var temp = (from q in ctx.Web_Base_UserAdmin
                            where q.FlagStatus != -1 && q.Name == user.Name
                            select q).FirstOrDefault();
                if (temp == null) return accx;
                accx.PkId = temp.PkId;
                accx.IP = Request.ServerVariables["Remote_Url"];
                accx.Name = temp.Name;
                accx.NameAs = temp.Name;
                accx.NameTrue = temp.Name;
                accx.NameFull = temp.Name;
                accx.Mobile = temp.Name;
                accx.Mail = temp.Name;
                accx.TypeSystem = user.TypeSystem;*/
            }
            //return accx;
        }
    }
}
